<template>
	<view class="body">
		<view class="input-box">
			<u-search placeholder="请输入姓名搜索" v-model="searchValue" @clear="clearSearch" @change="doSearch"
				:showAction="false" :actionStyle="actionStyle">
			</u-search>
		</view>
		<view class="person-List" v-for="(item,index) in userList" v-show="item.show" :key="index" @click="gotoweex(item)" :style='{"margin-bottom":index==userList.length-1?"50rpx":"0"}' >
			<view class="list-top" @click="radioClick(item)" style="margin-bottom: ;">
				<!-- 多选 :checked="item.checked || flag==index"-->
				<view class="">
					<label class="radio">
						<radio :value="item.userId" :checked="item.checked" />
					</label>
				</view>

				<view class="list-list">
					<view class="">
						<u-avatar :text="item.userName.slice(1)" :fontSize="item.userName.length > 2?15:18"
							shape="square" bgColor="#0087FC">
						</u-avatar>
					</view>
					<!-- <view class="list-business">
						<text class="list-business-title">{{item.userName.slice(0,2)}}</text>
					</view> -->
					<view class="list-business-name">
						<text class="list-business-bigname">{{item.userName}}</text>
					</view>
				</view>
			</view>
		</view>
		<view class="notYetclass" v-if="notYet">
			<text class="notYetclass-text">抱歉，暂未找到</text>
		</view>
		<view class="list-bottom">
			<view class="list-bottom-all-left">
				<view class="list-bottom-lianxi" @click='open(isChange.length)'>
					<text class="list-bottom-queding">已选择:{{isChange.length}}人</text>
					<view style="margin-left: 20rpx;" v-if="isChange.length > 0">
						<u-icon :bold="true" color="#2979ff" name="arrow-up"></u-icon>
					</view>
				</view>
				<text class="list-bottom-left" v-if="isChange.length<6" style="direction: ltr;white-space: nowrap;">
					<text class="list-uname" v-for='(it,i) in isChange' :key='i'>
						<text class="" v-if="isChange.length==1">{{it.userName}}</text>
						<text class="" v-else>{{it.userName+'、'}}</text>
					</text>
				</text>
				<text class="list-bottom-left" v-else
					style="direction: rtl;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;">
					<text class="list-uname" v-for='(it,i) in isChange' :key='i'>
						<text class="" v-if="isChange.length==1">{{it.userName}}</text>
						<text class="" v-else>{{it.userName+'、'}}</text>
					</text>
				</text>
			</view>
			<view @click="doSubmit">
				<view style="width: 180rpx;height: 60rpx;color: #FFF;font-size: 28rpx;background-color: #2979ff;line-height: 60rpx; margin-right: 30rpx;text-align: center;border-radius: 12rpx;">确定({{isChange.length}}/{{total}})</view>
				<!-- <button type="primary"
					style="width: 180rpx;height: 60rpx;background-color: #2979ff;border: none;line-height: 60rpx; margin-right: 30rpx;">
					<text style="color: #FFF;font-size: 28rpx;">确定({{isChange.length}}/{{total}})</text>
				</button> -->
			</view>
		</view>

		<view class="" style="margin-bottom: -120rpx;">

		</view>
		<!-- 底部弹框 -->
		<uni-popup id="popup" ref="popup" type="bottom" :animation="false">
			<view class="popup-content"
				:style='{height:isChange.length>3?"auto":"500rpx"}'>
				<view class="" style='flex-direction: row;justify-content: space-between;height: 100rpx;display: flex;align-items: center;border-bottom: 2rpx solid #f0f0f0;'>
					<view class="" style='margin-left: 40rpx;'>
						<text class="list-bottom-queding" style="color: #333;">已选择:{{isChange.length}}人</text>
					</view>
					<view class="" @click='submit'>
						<text style='margin-right:10px;color:#007AFF;font-size: 32rpx;'>确认</text>
					</view>
				</view>
				<!-- <view
					style='flex-direction: row;border-bottom-width: 1px;border-bottom-color: #f0f0f0;width: 750rpx;justify-content: space-between;'>
				</view> -->
				<scroll-view scroll-y="true" class="persons-scoll" @touchmove.stop>
					<view class="" v-for='(it,i) in isChange' :key='i'
						style='flex-direction: row;border-bottom: 2rpx solid #f0f0f0; width: 750rpx;justify-content: space-between;height: 100rpx;display: flex;align-items: center;'>
						<view class="" style='position: relative; display: flex; align-items: center;'>
							<view style='position: absolute; left: 20px;'>
								<u-avatar :text="it.userName.slice(1)" :fontSize="it.userName.length > 2?12:15"
									shape="square" size="35" bgColor="#0087FC">
								</u-avatar>
							</view>
							<!-- <text class="avatar" v-if='it.uname' style='position: absolute;left:20px'>{{it.uname.length>2?it.uname.substr(it.uname.length-2):it.uname}}</text> -->
							<text
								style='margin-left: 130rpx;color: #333;font-size: 36rpx;'>{{it.userName}}</text>
						</view>
						<view class="" style='margin-right: 20px'>
							<view
								style='font-size: 28rpx;text-align: center;border-radius: 10rpx;width: 100rpx;height: 50rpx;line-height: 50rpx; border: 2rpx solid #C9C9C9;'
								@click.stop='deleteUser(it)'>移除</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import uniPopup from './uni-popup/uni-popup.vue';
	import {
		userList
	} from '@/api/user.js'
	export default {
		data() {
			return {
				actionStyle: {
					color: '#007AFF',
					fontWeight: 'bold',
				},
				searchValue: '',
				total: 0,
				userList: [],
				isChange: [], //多选集合
				notYet: false,
				callBackFunc: '',
				chooseUsers:[],
				chooseType: '多选',
				// flag:'-1'
			}
		},
		onLoad(option) {
			this.getUsers()
			this.isChange = [];
			this.type = option.type;
			this.chooseType = option.chooseType;
			this.chooseUsers = option.chooseUsers && JSON.parse(decodeURIComponent(option.chooseUsers))
		},
		components: {
			uniPopup
		},
		methods: {
			doSubmit() {
				let pages = getCurrentPages();
				let prevPage = pages[pages.length - 2];
				prevPage.$vm.t = 'choose'
				prevPage.$vm.type = this.type
				if (this.type == "审批人") {
					prevPage.$vm.setUsers(this.isChange);
				}else if (this.type == "抄送人") {
					prevPage.$vm.setCc(this.isChange);
				}else if (this.type == "质检人") {
					prevPage.$vm.setZj(this.isChange);
				}else if (this.type == "任务管家参与人"||this.type == '成果会签测量人员') {
					prevPage.$vm.setChooseUsers(this.isChange);
				}else if (this.type == "任务管家审批人") {
					prevPage.$vm.setChooseSpUsers(this.isChange);
				}else if (this.type == "物资管理") {
					prevPage.$vm.setChooseUsers(this.isChange);
				}else if (this.chooseType == "单选") {
					prevPage.$vm.setChooseUser(this.isChange);
				}else if (this.chooseType == "多选") {
					prevPage.$vm.setChooseUsersCgw(this.isChange);
				}
				uni.navigateBack();
			},
			doSearch() {
				if (this.searchValue === '') {
					this.clearSearch()
				}
				this.userList.map(e => {
					if (e.userName.indexOf(this.searchValue) > -1) {
						e.show = true
					} else {
						e.show = false
					}
				})
			},
			clearSearch() {
				this.searchValue = ''
				this.userList.map(e => e.show = true)
			},
			async getUsers() {
				const {
					data
				} = await userList();
				if (data.code == 200) {
					this.userList = data.data
					this.total = data.data.length
					let chooseUsers = this.chooseUsers.map(e=>e.userId)
					// this.userList.map(e => e.show = true)
					this.userList.forEach(e=>{
						if(chooseUsers.indexOf(e.userId)>-1){
							e.checked = true;
							this.isChange.push(e);
						}
						e.show = true
					})
				}
			},
			radioClick(item) {
				if(this.chooseType == '多选'){
					// 多选
					if (item.checked) {
						item.checked = false;
						this.isChange.splice(this.isChange.indexOf(item), 1);
					} else {
						item.checked = true;
						this.isChange.push(item);
					}
				}else if(this.chooseType == '单选'){
					if (!item.checked) {
						this.userList.map(e=>e.checked = false)
						item.checked = true;
						this.isChange = [item];
					}
				}else{
					// 多选
					if (item.checked) {
						item.checked = false;
						this.isChange.splice(this.isChange.indexOf(item), 1);
					} else {
						item.checked = true;
						this.isChange.push(item);
					}
				}
			},
			// radioClick(item,index) {
			// 	console.log(index)
			// 	console.log(this.chooseType)
			// 	// 多选
			// 	if (item.checked || this.flag == index) {
			// 		item.checked = false;
			// 		this.isChange.splice(this.isChange.indexOf(item), 1);
			// 		this.flag = '-1'
			// 	} else {
			// 		if(this.chooseType=='true') {
			// 			this.flag = index
			// 			this.isChange = []
			// 		}else {
			// 			item.checked = true
			// 		}
			// 		this.isChange.push(item);
			// 	}
			// 	console.log(item.checked)
			// 	console.log(this.flag)
			// },
			deleteUser(it) {
				this.userList.forEach(its => {
					if (its.userId == it.userId) {
						its.checked = false
					}
				})
				this.isChange = this.isChange.filter(item => {
					return !it.userId.includes(item.userId);
				})
			},
			// 弹窗打开
			open(selectCount) {
				if (selectCount < 1) return
				let that = this;
				that.$refs.popup.open();
			},
			// 弹窗关闭
			submit() {
				this.$refs.popup.close();
			},
			gotoweex(item) {
				let arr = [];
				arr.push({
					id: item.uid || item.userId
				})
				this.clearSearch()
			},
		}
	}
</script>

<style>
	.body {
		/* padding: 20rpx 25rpx; */
		padding-bottom: 180rpx;
	}

	.input-box {
		/* width: 750rpx; */
		margin: 0 auto;
		padding: 10rpx 30rpx;
		background-color: #ffffff;
	}
	
	.person-List {
		border-bottom: 1px solid #ccc;
		margin: 0 20rpx;
	}

	.input {
		padding-left: 30rpx;
		background-color: #f0f0f0;
		border-radius: 10rpx;
		font-size: 28rpx;
		height: 70rpx;
	}

	.list-top {
		/* margin-top: 30rpx; */
		background-color: #0067D8;
		height: 130rpx;
		background-color: #FFFFFF;
		padding: 15rpx 10px;
		flex-direction: row;
		display: flex;
		align-items: center;
	}

	.list-list {
		flex-direction: row;
		flex: 1;
		display: flex;
		align-items: center;
	}

	.radio {
		/* line-height: 330rpx; */
		padding-top: 25rpx;
		padding-right: 20rpx;
	}

	.list-business-title {
		color: #FFFFFF;
		text-align: center;
		line-height: 100rpx;
	}

	.list-business {
		height: 100rpx;
		/* margin: 15rpx auto; */
		/* background-color: #007AFF; */
		width: 100rpx;
		/* border-radius: 10rpx; */

	}

	.list-business-name {
		margin-left: 20rpx;
	}

	.list-business-bigname {
		font-size: 28rpx;
		line-height: 100rpx;
	}

	.list-bigtop {
		height: 220rpx;
		background-color: #FFFFFF;
		margin-top: 20rpx;
		flex-direction: row;
	}

	.list-bigtop-a {
		width: 375rpx;
		text-align: center;
	}

	.list-bigtop-b {
		width: 375rpx;
	}

	.list-bigtop-text {
		font-size: 32rpx;
		/* line-height: 200rpx; */
		text-align: center;
		margin-top: 20rpx;
	}

	.list-bigtop-kang {
		text-align: center;
		width: 110rpx;
		height: 110rpx;
		margin-left: 130rpx;
		margin-top: 30rpx;
		background-color: #55aa00;
		border-radius: 30%;
		color: #FFFFFF;
		line-height: 100rpx;
	}

	.list-bottom {
		position: fixed;
		height: 100rpx;
		background-color: #FFFFFF;
		width: 750rpx;
		bottom: 0;
		left: 0;
		flex-direction: row;
		flex-wrap: nowrap;
		/* font-size: 20rpx; */
		justify-content: space-between;
		padding: 20rpx 20rpx;
		display: flex;
		align-items: center;
	}
	
	.list-bottom-all-left {
		display: flex;
		flex-direction: column;
	}

	.list-bottom-queding {
		color: #007AFF;
		font-size: 30rpx;
		font-weight: bold;
	}

	.list-bottom-lianxi {
		flex-direction: row;
		display: flex;
		align-items: center;
	}

	.notYetclass {
		width: 750rpx;
		height: 700rpx;
	}

	.notYetclass-text {
		text-align: center;
		line-height: 700rpx;
		color: #008CFF;
	}

	.popup-content {
		width: 750rpx;
		height: 500rpx;
		z-index: 99999;
		background-color: #fff
	}

	.avatar {
		width: 70rpx;
		height: 70rpx;
		background: #3489ff;
		text-align: center;
		line-height: 70rpx;
		border-radius: 15rpx;
		font-size: 26rpx;
		color: #fff;
	}

	.indexBak {
		width: 650rpx;
		text-align: right;
	}

	.indexBak_text {
		text-align: right;
		line-height: 80rpx;
		font-size: 36rpx;
	}

	.add-btn {
		width: 200rpx;
		padding: 10rpx;
		color: white;
		display: flex;
		justify-content: center;
		align-items: center;
		background-color: #1d2e8d;
		border-radius: 10rpx;
	}

	.list-bottom-left {
		width: 500rpx;
		/* position: fixed;
		flex-wrap: nowrap;
		flex-direction: row;
		justify-content: stretch;
		bottom: 0; */
	}

	.list-uname {
		flex-direction: row;
		font-size: 25rpx;
		color: #7a7a7a;
	}
	
	.persons-scoll {
		height: 800rpx;
	}
</style>
